<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.cyys.com/cyystaglib" prefix="cyys"%>
<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>${SYSTEM_NAME}</title>
	<link rel="stylesheet" href="${pageContext.request.contextPath}/static/layuiadmin/layui/css/layui.css?v=${FILE_VERSION}" media="all">
	<link rel="stylesheet" href="${pageContext.request.contextPath}/static/layuiadmin/style/admin.css?v=${FILE_VERSION}" media="all">
</head>
<body>
	<div class="layui-fluid">
		<div class="layui-card">
			<div class="layui-card-header">用户管理</div>
			<div class="layui-card-body">
				<div class="layui-form layer-query">
					<div class="layui-form-item">
						<div class="layui-inline">
							<label class="layui-form-label">用户名：</label>
							<div class="layui-input-inline">
								<input type="text" name="username"  id="username" autocomplete="off" class="layui-input">
							</div>
						 </div>
						<div class="layui-inline">
							<button class="layui-btn" onclick="reload()">查询</button>
						</div>
					</div>
				</div>
		  	<hr class="layui-bg-green">
		  	<div class="layui-btn-group">
				<button id="addLayer" class="layui-btn">新增</button>
				<button id="modi" class="layui-btn">编辑</button>
				<button id="deleteBatch" class="layui-btn layui-btn-danger">删除</button>
		  	</div>
		  	<table class="layui-table" lay-filter="tableList" id="layerTable">
		  	</table>
		</div>
	</div>

	<script type="text/html" id="roles">
		{{# for(var i = 0;i< d.roleList.length;i++){ }}
		<a class="layui-btn layui-btn-radius layui-btn-xs  layui-btn-warm"> {{ d.roleList[i].roleName }}</a>
		{{#}}}
	</script>
	
	<script type="text/html" id="barDemo">
		{{#if(d.id != '<shiro:principal property="id"/>'){ }}
			<a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="setRole">设置角色</a>
			<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
			<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
		{{# } }}
		<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="initPwd">初始化密码</a>
	</script>
</div>
	<script src="${pageContext.request.contextPath}/static/js/common-2.0.0.js?v=${FILE_VERSION}"></script>
	<script src="${pageContext.request.contextPath}/static/layuiadmin/layui/layui.js?v=${FILE_VERSION}"></script>
	<script src="${pageContext.request.contextPath}/static/js/jquery-3.3.1.min.js?v=${FILE_VERSION}"></script>
	<script src="${pageContext.request.contextPath}/static/js/common/core-2.0.0.js?v=${FILE_VERSION}"></script>
<script>
//JavaScript代码区域
var table;
var layer;
layui.config({
	base: getContextPath() + '/static/layuiadmin/' //静态资源所在路径
}).extend({
	index: 'lib/index' //主入口模块
}).use(['table','element','layer'], function(){
  var element = layui.element;
  layer = layui.layer;
  table = layui.table;
  table.render({ //其它参数在此省略
	  elem : '#layerTable',
	  even :"true",
	  limit: 10,
	  limits: [10,20,30],
	  id: 'tableList',
	  url: getContextPath() + '/sys/user/list/listInfo',
	  page:true, 
	  //where: {token: 'sasasas', id: 123} //如果无需传递额外参数，可不加该参数
	  method: 'post', //如果无需自定义HTTP类型，可不加该参数
	  cols:  [[ //标题栏
	      {checkbox: true,fixed: 'left'}
		  ,{field:'username', title:"登录账号",width: 200}
		  ,{field:'realName', width:120,title:"真实姓名"}
		  ,{field:'phone', width:120,title:"手机号"}
		  ,{field:'typeName', width:120,title:"用户类型"}
		  ,{minWidth:120,title:"已设置角色",templet: '#roles'}
         ,{fixed: 'right', width:300, align:'center',title:"操作", toolbar: '#barDemo'}
       ]],
	  request: {
		  pageName: 'current', //页码的参数名称，默认：page
		  limitName: 'size' //每页数据量的参数名，默认：limit
	  } //如果无需自定义请求参数，可不加该参数
	});
  
  //监听表格复选框选择
  table.on('checkbox(tableList)', function(obj){
    console.log(obj);
  });
  //监听工具条
  table.on('tool(tableList)', function(obj){
    var data = obj.data;
    if(obj.event === 'initPwd'){  //初始化密码
    	initPwd(data);
    } else if(obj.event === 'del'){
		deleteById(data,obj,'/sys/user/del/deleteById');
    } else if(obj.event === 'edit'){
		addLayer('/sys/user/edit/input', data.id);
    } else if(obj.event === 'setRole'){  //设置角色
    	setRole(data.id);
      }
  });

	$("#addLayer").click(function(){
		addLayer('/sys/user/add/input');
	});

	$("#modi").click(function(){
		var checkStatus = table.checkStatus('tableList')
				,data = checkStatus.data;
		if(data.length == 0 || data.length > 1){
			layer.msg("请选择一条数据");
			return false;
		}
		if(data[0].id == '<shiro:principal property="id"/>'){
			layer.msg("不允许编辑此管理员");
			return;
		}
		addLayer('/sys/user/edit/input',data[0].id);
	});

	$("#deleteBatch").click(function(){
		var checkStatus = table.checkStatus('tableList')
				,data = checkStatus.data;
		if(data.length == 0 ){
			layer.msg("请至少选择一条数据");
			return false;
		}else{
			for (var i = 0; i < data.length; i++) {
				if(data[i].id == '<shiro:principal property="id"/>'){
					layer.msg("不允许删除初始化的管理员");
					return;
				}
			}
		}
		deleteBatch('/sys/user/del/deleteByIds');
	});
});

function reload(){
	var username = $('#username');
    table.reload('tableList', {
  	  where: {
  		  username: username.val()
         }
    });
}



function initPwd(data){
	layer.confirm('真的要初始化此用户密码吗？', function(index){
		$ajax({
			url: "/sys/user/add/initPwd",
            data: {id : data.id},
            succCallback: function (json) {
            	layer.msg(json.msg);
            }
		});
	});
}

function setRole(id){
	var url  = getContextPath() +  '/sys/user/add/setRole?userId=' + id ;
	layer.open({
	  title : "设置角色",
	  type: 2,
	  area: ['500px', '400px'],
	  anim: 2,
	  content: [url, 'no']
	});
}
</script>
</body>
</html>